package com.iseastar.guojiang.performance;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.PerformanceBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.performance.adapter.PerformanceAdapter;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * Created by joker on 2017/8/2.
 * 我的业绩
 */
public class MyPerformanceActivity extends BaseActivity2 {

    private SListView mListView;
    private PerformanceAdapter mAdapter = null;
    private ReqResult<PerformanceBean> result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().queryMyPerformanceData(1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_performance_my);
        super.findViewById();
        getAppTitle().setCommonTitle("我的业绩");
        mListView = (SListView) findViewById(R.id.listview);
        mAdapter = new PerformanceAdapter(null, getContext());
        mListView.setAdapter(mAdapter);

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().queryMyPerformanceData(1);
            }
        });
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().queryMyPerformanceData(result.getPageNum() + 1);
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PerformanceBean bean = mAdapter.getItem(position);
                if (bean == null) {
                    return;
                }
                Intent intent = new Intent(getContext(), PerformanceDetailActivity.class);
                intent.putExtra("date", bean.getDate());
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //我的业绩数据返回
            case MsgID.query_my_performance:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, PerformanceBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            if (result.getPageNum() == 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    public void onclick(PerformanceBean bean) {
        Intent intent = new Intent(getContext(), PerformanceDetailActivity.class);
        intent.putExtra("date", bean.getDate());
        startActivity(intent);
    }
}
